package com.htandroid.htandroid.excel;

import android.content.Context;

import com.htandroid.htandroid.entity.Htzc_Pvouch;
import com.htandroid.htandroid.global.MyApplication;

import java.io.File;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExcelAsset_zcpd extends BaseExcel {
    private static WritableWorkbook wwb;
    private static String excelPath;
    private static String listtype;
    private static File excelFile;
    private static List<Htzc_Pvouch> list;

    public ExcelAsset_zcpd(Context context, String excelPath, List<Htzc_Pvouch> list, String listtype) {
        this.excelPath = excelPath;
        this.list = list;
        this.listtype = listtype;
        excelFile = new File(excelPath);
        myApplication = (MyApplication) context.getApplicationContext();
        createSmtrautExcel(myApplication, excelFile, list, listtype);
    }

    public static void createSmtrautExcel(MyApplication myApplication, File file, List<Htzc_Pvouch> list, String listtype) {
        format();
        WritableSheet ws = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            wwb = Workbook.createWorkbook(file);
            ws = wwb.createSheet("资产盘点", 0);

            Label label = new Label(0, 0, myApplication.getGs_ztname(), arial14format);
            ws.addCell(label);

            String[] topic = {"单据号", "盘点日期", "盘点部门"};
            for (int i = 0; i < topic.length; i++) {
                Label labelC = new Label(i, 1, topic[i], arial12format);
                try {
                    ws.addCell(labelC);
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }

            for (int i = 0; i < list.size(); i++) {
                Label labelCid = new Label(0, i + 2, list.get(i).getCvouchid());
                Label labelCdate = new Label(1, i + 2, list.get(i).getDdate());
                Label labelSdepts = new Label(2, i + 2, list.get(i).getSdepts());
                try {
                    ws.addCell(labelCid);
                    ws.addCell(labelCdate);
                    ws.addCell(labelSdepts);
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }
            ws.mergeCells(0, 0, topic.length - 1, 0);
            ws.setColumnView(0, 15);
            ws.setColumnView(1, 20);
            ws.setColumnView(2, 15);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (wwb != null) {
                try {
                    wwb.write();
                    wwb.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
